<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { useUserStore } from '@/stores/usersStore'
import { registerAPI } from '@/apis/user';
import { ElMessage } from 'element-plus';
import 'element-plus/theme-chalk/el-message.css'

const router = useRouter()
const formRef = ref(null)
const flag = ref(true)
const form = ref({
    username: '',
    password: '',
    rePassword: '',
    agree: false
})

const rules = {
    username: [
        { required: true, message: '用户名不能为空！', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '密码不能为空', trigger: 'blur' },
        { min: 6, max: 14, message: '密码为6-14位长度', trigger: 'blur' },
        {
            validator: (rule, value, callback) => {
                // 同时包含数字和字母的正则表达式
                const hasNumber = /\d/;
                const hasLetter = /[a-zA-Z]/;

                if (!hasNumber.test(value) || !hasLetter.test(value)) {
                    callback(new Error('密码必须包含数字和字母'));
                } else {
                    callback();
                }
            },
            trigger: 'blur'
        }
    ],
    rePassword: [
        { required: true, message: '密码不能为空', trigger: 'blur' },
        {
            validator: (rule, value, callback) => {
                if (value != form.value.password) {
                    callback(new Error('两次输入的密码不一致'));
                } else {
                    callback();
                }
            },
            trigger: 'blur'
        }
    ],
    agree: [
        {
            validator: (rule, value, callback) => {
                if (value) {
                    callback();
                } else {
                    callback(new Error('请同意协议'));
                }
            }
        }
    ]
}
const userStore = useUserStore()
//统一登录校验
const toLogin = () => {
    const { username, password } = form.value
    formRef.value.validate(async (valid) => {
        if (valid) {
            await userStore.getUserInfo({ username, password })
            //登录成功
            ElMessage({ type: 'success', message: '登录成功，欢迎' + username + '回来' })
            router.replace({ path: '/' })
        }
    })
}
const toRegister = () => {
    const { username, password, rePassword } = form.value
    formRef.value.validate(async (valid) => {
        if (valid) {
            const res = await registerAPI({ username, password, rePassword })
            //注册成功
            userStore.userInfo = res.data
            ElMessage({ type: 'success', message: '注册成功！' })
            router.replace({ path: '/' })
        }
    })
}
const centerDialogVisible = ref(false)
</script>


<template>
    <div>
        <header class="login-header">
            <div class="container m-top-20">
                <h1 class="logo">
                    <RouterLink to="/">潮鞋电商平台</RouterLink>
                </h1>
                <h1 style="margin-left: -800px;margin-bottom: 30px;"><a href="/">潮鞋电商平台</a></h1>
                <RouterLink class="entry" to="/">
                    进入网站首页
                    <i class="iconfont icon-angle-right"></i>
                    <i class="iconfont icon-angle-right"></i>
                </RouterLink>
            </div>
        </header>
        <el-dialog v-model="centerDialogVisible" title="关于本项目" width="500" center>
            <span>
                本项目是基于前端是小鲜兔电商为原型制作而来。素材来源是由互联网、阿迪达斯等电商网站搜集而来
                如有侵权，联系后删除。
            </span>
            <div style="padding-top: 15px;">
                <el-button type="primary" @click="centerDialogVisible = false">
                    确定
                </el-button>
            </div>
        </el-dialog>
        <template v-if="flag">
            <section class="login-section">
                <div class="wrapper">
                    <nav>
                        <a href="javascript:;">账户登录</a>
                    </nav>
                    <div class="account-box">
                        <div class="form">
                            <el-form label-position="right" label-width="60px" status-icon :model="form" :rules="rules"
                                ref="formRef">
                                <el-form-item label="账户" prop="username">
                                    <el-input v-model="form.username" placeholder="请输入账户" />
                                </el-form-item>
                                <el-form-item label="密码" prop="password">
                                    <el-input v-model="form.password" type="password" show-password
                                        placeholder="请输入6-14位“数字字母组合“密码" />
                                </el-form-item>
                                <el-form-item label-width="22px" prop="agree">
                                    <el-checkbox size="large" v-model="form.agree"
                                        @click.once="centerDialogVisible = true">
                                        我已同意隐私条款和服务条款
                                    </el-checkbox>
                                </el-form-item>
                                <el-button size="large" class="subBtn" @click="toLogin">点击登录</el-button>
                                <a style="float: right;margin-top: 22px;" @click="flag = false">还没有账户，前往注册>></a>
                            </el-form>
                        </div>
                    </div>
                </div>
            </section>
        </template>
        <template v-else>
            <section class="login-section">
                <div class="wrapper">
                    <nav>
                        <a href="javascript:;">账户注册</a>
                    </nav>
                    <div class="account-box">
                        <div class="form">
                            <el-form label-position="right" label-width="80px" status-icon :model="form" :rules="rules"
                                ref="formRef">
                                <el-form-item label="账户" prop="username">
                                    <el-input v-model="form.username" placeholder="请输入账户" />
                                </el-form-item>
                                <el-form-item label="密码" prop="password">
                                    <el-input v-model="form.password" placeholder="请输入6-14位“数字字母组合“密码" type="password"
                                        show-password />
                                </el-form-item>
                                <el-form-item label="确认密码" prop="rePassword">
                                    <el-input v-model="form.rePassword" placeholder="请再次确认密码" type="password"
                                        show-password />
                                </el-form-item>
                                <el-form-item label-width="22px" prop="agree">
                                    <el-checkbox size="large" v-model="form.agree"
                                        @click.once="centerDialogVisible = true">
                                        我已同意隐私条款和服务条款
                                    </el-checkbox>
                                </el-form-item>
                                <el-button size="large" class="subBtn" @click="toRegister">点击注册</el-button>
                                <a style="float: right;margin-top: 22px;" @click="flag = true">已有账户，前往登录>></a>
                            </el-form>
                        </div>
                    </div>
                </div>
            </section>
        </template>

        <footer class="login-footer">
            <div class="container">
                <p>
                    <a href="javascript:;">关于我们</a>
                    <a href="javascript:;">帮助中心</a>
                    <a href="javascript:;">售后服务</a>
                    <a href="javascript:;">配送与验收</a>
                    <a href="javascript:;">商务合作</a>
                    <a href="javascript:;">搜索推荐</a>
                    <a href="javascript:;">友情链接</a>
                </p>
                <p><a title="点我前往gitee仓库" href="https://gitee.com/mcyuanfang/fashion-shoes-front">CopyRight ©
                        潮鞋电商平台</a></p>
            </div>
        </footer>
    </div>
</template>

<style scoped lang='scss'>
.login-header {
    background: #fff;
    border-bottom: 1px solid #e4e4e4;

    .container {
        display: flex;
        align-items: flex-end;
        justify-content: space-between;
    }

    .logo {
        width: 200px;

        a {
            display: block;
            height: 132px;
            width: 100%;
            text-indent: -9999px;
            background: url("@/assets/images/logo.ico") no-repeat center 18px / contain;
        }
    }

    .sub {
        flex: 1;
        font-size: 24px;
        font-weight: normal;
        margin-bottom: 38px;
        margin-left: 20px;
        color: #666;
    }

    .entry {
        width: 120px;
        margin-bottom: 38px;
        font-size: 16px;

        i {
            font-size: 14px;
            color: $xtxColor;
            letter-spacing: -5px;
        }
    }
}

.login-section {
    background: url('@/assets/images/login-bg.png') no-repeat center / cover;
    height: 488px;
    position: relative;

    .wrapper {
        width: 380px;
        background: #fff;
        position: absolute;
        left: 50%;
        top: 54px;
        transform: translate3d(100px, 0, 0);
        box-shadow: 0 0 10px rgba(0, 0, 0, 0.15);

        nav {
            font-size: 14px;
            height: 55px;
            margin-bottom: 20px;
            border-bottom: 1px solid #f5f5f5;
            display: flex;
            padding: 0 40px;
            text-align: right;
            align-items: center;

            a {
                flex: 1;
                line-height: 1;
                display: inline-block;
                font-size: 18px;
                position: relative;
                text-align: center;
            }
        }
    }
}

.login-footer {
    padding: 30px 0 50px;
    background: #fff;

    p {
        text-align: center;
        color: #999;
        padding-top: 20px;

        a {
            line-height: 1;
            padding: 0 10px;
            color: #999;
            display: inline-block;

            ~a {
                border-left: 1px solid #ccc;
            }
        }
    }
}

.account-box {
    .toggle {
        padding: 15px 40px;
        text-align: right;

        a {
            color: $xtxColor;

            i {
                font-size: 14px;
            }
        }
    }

    .form {
        padding: 0 20px 20px 20px;

        &-item {
            margin-bottom: 28px;

            .input {
                position: relative;
                height: 36px;

                >i {
                    width: 34px;
                    height: 34px;
                    background: #cfcdcd;
                    color: #fff;
                    position: absolute;
                    left: 1px;
                    top: 1px;
                    text-align: center;
                    line-height: 34px;
                    font-size: 18px;
                }

                input {
                    padding-left: 44px;
                    border: 1px solid #cfcdcd;
                    height: 36px;
                    line-height: 36px;
                    width: 100%;

                    &.error {
                        border-color: $priceColor;
                    }

                    &.active,
                    &:focus {
                        border-color: $xtxColor;
                    }
                }

                .code {
                    position: absolute;
                    right: 1px;
                    top: 1px;
                    text-align: center;
                    line-height: 34px;
                    font-size: 14px;
                    background: #f5f5f5;
                    color: #666;
                    width: 90px;
                    height: 34px;
                    cursor: pointer;
                }
            }

            >.error {
                position: absolute;
                font-size: 12px;
                line-height: 28px;
                color: $priceColor;

                i {
                    font-size: 14px;
                    margin-right: 2px;
                }
            }
        }

        .agree {
            a {
                color: #069;
            }
        }

        .btn {
            display: block;
            width: 100%;
            height: 40px;
            color: #fff;
            text-align: center;
            line-height: 40px;
            background: $xtxColor;

            &.disabled {
                background: #cfcdcd;
            }
        }
    }

    .action {
        padding: 20px 40px;
        display: flex;
        justify-content: space-between;
        align-items: center;

        .url {
            a {
                color: #999;
                margin-left: 10px;
            }
        }
    }
}

.subBtn {
    background: $xtxColor;
    width: 100%;
    color: #fff;
}
</style>